package com.hethongviet.preventingmachine.page;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.hethongviet.preventingmachine.Constants;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.SMSHelper;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessageBack;

public class PageChangeNumberActivity extends Activity
{
	private String _newNumber = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		SMSHelper.setActivity(this);

		setContentView(R.layout.page_change_number);

		final String oldNumber = getIntent().getStringExtra(
				Constants.EXTRA_NUMBER);

		final EditText editTextNumber = (EditText) findViewById(R.id.change_edit_text_number);
		editTextNumber.setText(oldNumber);

		final Button buttonAccept = (Button) findViewById(R.id.change_button_accept);
		final Button buttonCancel = (Button) findViewById(R.id.change_button_cancel);

		buttonAccept.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0)
			{
				_newNumber = editTextNumber.getText().toString();
				if (_newNumber == null || _newNumber.equals(""))
				{
					editTextNumber.setText("");
					showDialogWithCatch(R.string.message_error_input_null);
					return;
				}

				if (_newNumber.length() < Constants.LENGTH_MIN_NUMBER)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_number);
					return;
				}

				if (_newNumber.length() > Constants.LENGTH_MAX_NUMBER)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_number);
					return;
				}

				if (_newNumber.equals(oldNumber))
				{
					showDialogWithCatch(R.string.message_error_input_number_same_old);
					return;
				}

				for (int index = 0; index < _newNumber.length(); index++)
				{
					char c = _newNumber.charAt(index);

					if (!Character.isDigit(c) && c != '+')
					{
						showDialogWithCatch(R.string.message_error_wrong_input_number);
						return;
					}
				}

				/* Save number */
				Intent intent = new Intent();
				intent.putExtra(Constants.EXTRA_NUMBER, _newNumber);

				setResult(RESULT_OK, intent);
				finish();
			}
		});
		buttonCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0)
			{
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	private void showDialogWithCatch(int id)
	{
		try
		{
			showDialog(id);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == R.string.message_error_input_null
				|| id == R.string.message_error_input_number_same_old
				|| id == R.string.message_error_wrong_input_number)
		{
			return new DialogMessageBack(this, true, id, false);
		}
		else if (id == R.string.message_error_input_too_long_number)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_NUMBER), false);
		}
		else if (id == R.string.message_error_input_too_short_number)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_NUMBER), false);
		}
		else return super.onCreateDialog(id);
	}

}
